﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Utils.NPOIHelp
{
    /// <summary>
    /// TaskPanel.xaml 的交互逻辑
    /// </summary>
    public partial class TaskPanel : UserControl
    {
        public TaskPanel(后台任务 task)
        {
            InitializeComponent();

            this.task = task;
            taskName.Text = task.任务名称;
        }

        private 后台任务 task;

        public 后台任务 Task
        {
            get { return task; }
            set { task = value; }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            var tip = string.IsNullOrWhiteSpace(taskName.Text) ? "此任务" : $"【{taskName.Text}】";
            var answer = LMessageBox.ShowWarning($"真的要取消{tip}吗？");

            if (answer != MessageBoxResult.Yes) return;

            if(task != null)
            {
                task.取消任务();
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            if(task == null)
            {
                if (Globals.MainWindow.TaskListPanel.Items.Contains(this))
                {
                    Globals.MainWindow.TaskListPanel.Items.Remove(this);
                }
                return;
            }

            if ((task.是否任务正忙 || task.是否已完成任务 == false) && task.是否已取消任务 == false)
            {
                LMessageBox.ShowWarning("任务尚未完成！实在要关闭，请先取消任务！");
                return;
            }
            else
            {
                if (Globals.MainWindow.TaskListPanel.Items.Contains(this))
                {
                    Globals.MainWindow.TaskListPanel.Items.Remove(this);
                }
            }
        }

        private void btnShowMessageBox_Click(object sender, RoutedEventArgs e)
        {
            LMessageBox.ShowInfomation("[auto]" + this.task.SbLog.ToString());
        }

        private void pbTaskProcess_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            tbTip.Text = $"{pbTaskProcess.Value} / {pbTaskProcess.Maximum}";
        }
    }
}
